package yunhe.dao.impl;

import yunhe.dao.AdminDao;
import yunhe.entity.AdminInfo;
import yunhe.factory.BaseConn;
import yunhe.util.NewsUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminDaoImpl extends BaseConn implements AdminDao {
	
	public AdminInfo login(String naName,String naPass) {
		String MD5=NewsUtil.str2md5(naName+naPass);
		AdminInfo re=new AdminInfo();
		String sql="select * from n_admin where napass=?";
		PreparedStatement pst=(PreparedStatement) setParams(sql, new String[]{MD5}, 1);
		try {
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
				re.setAnId(rs.getInt("NAID"));
				re.setnaName(naName);
				re.setnState(rs.getInt("NSTATE"));
			}
			else{
				re=null;
			}
		} catch (SQLException e) {
			re=null;
		}
		return re;
	}
	
	public int register(String naName,String naPass){
		String MD5=NewsUtil.str2md5(naName+naPass);
		int re=-1;
		String sql="insert into n_admin values(null,?,?,0)";
		Object[] params={naName,MD5};
		re=CUD(sql, params);
		return re;
	}

	
	public AdminInfo login(String MD5) {
		
		return null;
	}

}
